import 'package:flutter/material.dart';
import '../../models/page_state.dart';
import 'loading_widget.dart';
import 'empty_state_widget.dart';
import 'error_state_widget.dart';

/// 通用页面状态处理组件
/// 根据 PageState 显示对应的 UI
class StateHandlerWidget extends StatelessWidget {
  final PageState state;
  final String? errorMessage;
  final Widget child;
  final VoidCallback? onRetry;
  final String? emptyMessage;
  final IconData? emptyIcon;
  final String? loadingMessage;
  
  const StateHandlerWidget({
    super.key,
    required this.state,
    required this.child,
    this.errorMessage,
    this.onRetry,
    this.emptyMessage,
    this.emptyIcon,
    this.loadingMessage,
  });
  
  @override
  Widget build(BuildContext context) {
    switch (state) {
      case PageState.loading:
        return LoadingWidget(message: loadingMessage);
      case PageState.empty:
        return EmptyStateWidget(
          message: emptyMessage ?? '暂无数据',
          icon: emptyIcon ?? Icons.inbox,
          onRetry: onRetry,
        );
      case PageState.error:
        return ErrorStateWidget(
          message: errorMessage ?? '加载失败',
          onRetry: onRetry ?? () {},
        );
      case PageState.success:
      case PageState.noMore:
      case PageState.idle:
      default:
        return child;
    }
  }
}